spb/chat-spboucher-ai
Public
1import { z } from "zod";2import { guarded, json, body } from "@/lib/api";3import { sql } from "@/lib/db";45export const GET = guarded<{ id: string }>(async (_req, { params }) => {6 const p = await sql`SELECT * FROM projects WHERE id = ${params.id}`;7 if (!p[0]) return json({ error: "Introuvable" }, { status: 404 });8 const files = await sql`SELECT id, filename, mime, size_bytes, page_count, created_at FROM attachments WHERE project_id = ${params.id} ORDER BY created_at DESC`;9 const memories = await sql`SELECT id, category, content, status, created_at FROM memories WHERE project_id = ${params.id} ORDER BY created_at DESC`;10 const artifacts = await sql`SELECT id, title, type, language, version, created_at FROM artifacts WHERE project_id = ${params.id} AND saved ORDER BY created_at DESC`;11 return json({ project: p[0], files, memories, artifacts });12});1314export const PATCH = guarded<{ id: string }>(async (req, { params }) => {15 const b = await body(req, z.object({ name: z.string().min(1).max(120).optional(), description: z.string().max(2000).nullable().optional(), instructions: z.string().max(20_000).nullable().optional(), defaultModel: z.string().nullable().optional(), color: z.string().max(20).nullable().optional(), icon: z.string().max(20).nullable().optional(), notes: z.string().max(100_000).nullable().optional(), toolPermissions: z.record(z.string(), z.unknown()).optional(), archived: z.boolean().optional() }));16 await sql`UPDATE projects SET17 name = COALESCE(${b.name ?? null}, name),18 description = CASE WHEN ${b.description !== undefined} THEN ${b.description ?? null} ELSE description END,19 instructions = CASE WHEN ${b.instructions !== undefined} THEN ${b.instructions ?? null} ELSE instructions END,20 default_model = CASE WHEN ${b.defaultModel !== undefined} THEN ${b.defaultModel ?? null} ELSE default_model END,21 color = CASE WHEN ${b.color !== undefined} THEN ${b.color ?? null} ELSE color END,22 icon = CASE WHEN ${b.icon !== undefined} THEN ${b.icon ?? null} ELSE icon END,23 notes = CASE WHEN ${b.notes !== undefined} THEN ${b.notes ?? null} ELSE notes END,24 tool_permissions = COALESCE(${b.toolPermissions ? sql.json(b.toolPermissions as never) : null}, tool_permissions),25 archived_at = CASE WHEN ${b.archived === undefined} THEN archived_at WHEN ${b.archived ?? false} THEN now() ELSE NULL END26 WHERE id = ${params.id}`;27 const p = await sql`SELECT * FROM projects WHERE id = ${params.id}`;28 return json({ project: p[0] });29});3031export const DELETE = guarded<{ id: string }>(async (_req, { params }) => { await sql`DELETE FROM projects WHERE id = ${params.id}`; return json({ ok: true }); });32